<?php

/**
 * 阿里云 生活物联网接口测试
 */

namespace app\shell;

use app\service\AliyunProxyService;
use app\service\AliyunService;
use share\console\CConsole;
use share\console\CLIRunner;

class CAliyunApiTest extends CLIRunner
{
    public static  $projectId = 'a123yDt0dd5TecR9';
    public static  $regoin = 'CN';

    public function del_token_action(){
        $aliyunService = AliyunService::instance(self::$projectId,self::$regoin);
        $aliyunService->delCacheToken();
    }

    public function get_project_list_action()
    {
        $aliyunService = AliyunService::instance(self::$projectId,self::$regoin);
        return $aliyunService->getRawProductList(1,10);
    }

    public function get_project_detail_action($productKey = 'a11iBlJMVXz')
    {
        if(empty($productKey)){
            $productKey = CConsole::prompt("input the product key","");
        }
        $aliyunService = AliyunService::instance(self::$projectId,self::$regoin);
        return $aliyunService->getRawProductDetail($productKey);
    }

    public function get_device_list_action(){
        $aliyunService = AliyunService::instance(self::$projectId,self::$regoin);
        $productKey = 'a11iBlJMVXz';
        $status = 1;
        return $aliyunService->getRawListOfThings($productKey,$status,1,100);
    }

    public function get_device_regoin_action(){
        $aliyunService = AliyunService::instance(self::$projectId,self::$regoin);
        $productKey = 'a11iBlJMVXz';
        $deviceName = '9O047Xbv3hrk5xQXX3yQ';
        return $aliyunService->getRawThingRegoin($productKey,$deviceName);
    }

    public function get_device_info_action(){
        $aliyunService = AliyunService::instance(self::$projectId,self::$regoin);
        $productKey = 'a11iBlJMVXz';
        $deviceName = '9O047Xbv3hrk5xQXX3yQ';
        $iotId = 'jTAUbuWc1ku7MbyZ6VdK000000';
        // return $aliyunService->getRawThingInfo('',$productKey,$deviceName);
        return $aliyunService->getRawThingInfo($iotId);
    }


    public function get_user_list_action(){
        $aliyunService = AliyunService::instance(self::$projectId,self::$regoin);
        $offset = 1;
        $count = 10;
        return $aliyunService->getRawUserList($offset,$count);
    }

    public function get_user_device_action(){
        $aliyunService = AliyunService::instance(self::$projectId,self::$regoin);
        $identityId = '5004opd03db0fcb1c08209e1de84b26f010a5296';
        return $aliyunService->getUserDevice($identityId);
    }

    public function get_device_relation_user_action(){
        $aliyunService = AliyunService::instance(self::$projectId,self::$regoin);
        $iotId = 'jTAUbuWc1ku7MbyZ6VdK000000';
        return $aliyunService->getRawUserGetBindDevice($iotId);
    }

    public function async_by_device_name_action(){
        $params = [
            'productKey' => 'a11iBlJMVXz',
            'deviceName' => '9O047Xbv3hrk5xQXX3yQ',
        ];
        return  AliyunProxyService::asyncDeviceBindInfoByDeviceName($params);
    }

    public function async_by_user_action(){
        $params = [
            'uid' => '123456',      
            'vendor' => 'ENZ', 
        ];
        return AliyunProxyService::asyncUserBindInfo($params);
    }

    /**
     * 解除设备上所有绑定关系
     */
    public function device_unbind_all_user_action(){
        $aliyunService = AliyunService::instance(self::$projectId,self::$regoin);
        // $aliyunService->getRawUserDeviceUnbindAll("fF7QrZxoL2zc6mAV9ncO000000");

        // $iotId = 'YfjYybdmYB6wniRRv3R1000000';
        // $deviceName = 'YfjYybdmYB6wniRRv3R1';
        // $productKey = '';

        $iotIds = [
            "qJIFCkScGdEECmmnC2CX000000",
            "kugvbRIrtTsYKX41OKcA000000",
            "wjTKFgbuH4delECg1rAB000000",
            "f69vbk4DtVqV4bXUbHPV000000",
            "APWPTAgtFWSvgDjTTIAi000000",
            "L0xOxFt6vJ3MBEP7NXe2000000",
            "v60mWDAlx2H55H4UZYK7000000",
            "emKT27gvayZilBjV1qMn000000",
            "kobqMvRzF9SndwkNLkKB000000",
            "mZHGONkOe690Hod3SwB8000000",
            "iN8cv2Q4psKxOZJd4PQQ000000",
            "JM5SIqqfPknb6G6jrgeb000000",
            "sCnnv0vhoocyIflpxhYm000000",
            "Pm4nmfOLpwX3th7iovpd000000",
            "iCpRGRhf3BHaj7s5s8TP000000",
            "kKKrdAF4d5WOZKJ6l8IN000000",            
        ];
        foreach($iotIds as $iotId){
            $aliyunService->getRawUserDeviceUnbindAll($iotId);
        }        
    }


    /**
     * 设备绑定用户
     */
    public function device_bind_user_action(){
        $aliyunService = AliyunService::instance(self::$projectId,self::$regoin);
        $identityId = '5004opd03db0fcb1c08209e1de84b26f010a5296';
        $iotId = 'WpC4u9buiw6rs57BCK0j000000';        
        return $aliyunService->userBatchBind($identityId,$iotId);
    }

    /**
     * 批量转移设备 region
     */
    public function change_region_action() {
        $pk = 'a14DlpdtDGF';
        $dn = ['DaaeRzwQeEtOo9PevPtn'];
        $bindingCheck = false;
        $pushDeviceRegionSwitch = true;

        $aliyunService = AliyunService::instance(self::$projectId,"SG");

        $res = $aliyunService->changeRegion($pk,$dn,$bindingCheck,$pushDeviceRegionSwitch);
        return $res;
    }
}
